package todo;

import se.lth.cs.realtime.*;
import se.lth.cs.realtime.event.*;
import done.*;

public class WashingController implements ButtonListener {
	private AbstractWashingMachine theMachine;
	private double theSpeed;
	TemperatureController tc;
	WaterController wc;
	SpinController sc;
	WashingProgram washingProgram;
	boolean interrupted;
	
	 private final long tcPeriod = 10000;
	 private final long wcPeriod = 1000;
	 private final long scPeriod = 1000;
	
	
	
    
    public WashingController(AbstractWashingMachine theMachine,double theSpeed) {
    	this.theMachine = theMachine;
    	this.theSpeed = theSpeed;
    	
    	theSpeed = 2;
    	tc = new TemperatureController(theMachine, (long) (tcPeriod / theSpeed));
    	wc = new WaterController(theMachine, (long) (wcPeriod / theSpeed));
    	sc = new SpinController(theMachine, (long) (scPeriod / theSpeed));
    	tc.start();
    	wc.start();
    	sc.start();
    	washingProgram = null;
    }


    public void processButton(int theButton) {
    	
    	switch (theButton) {
		case 0: 
				washingProgram.interrupt();//Stänger av allt...
				interrupted = true; //Enable program 3
			break;
		case 1: 
			
			if(!theMachine.isLocked()){
			washingProgram = new WashingProgram1(theMachine, theSpeed, tc, wc, sc);
			washingProgram.start();
			}
			break;
		case 2:
			if(!theMachine.isLocked()){
				washingProgram = new WashingProgram2(theMachine, theSpeed, tc, wc, sc);
				washingProgram.start();
				}
	
	break;
		case 3: 
			if(interrupted || washingProgram == null){ //Ifall 0 tryckts || efter strömavbrott
				washingProgram = new WashingProgram3(theMachine, theSpeed, tc, wc, sc);
				washingProgram.start();
				interrupted = false;
			}
			break;
		
		}

    }
}





























